package com.yanqu.road.server.cmd.decoration;

import com.yanqu.road.entity.enums.eDecorationType;
import com.yanqu.road.pb.decoration.DecorationProto;
import com.yanqu.road.pb.player.PlayerProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.player.DecorationModule;
import com.yanqu.road.server.gameplayer.module.player.PlayerModule;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.server.command.Cmd;


@Cmd(code = Protocol.S_DECORATION_CHANGE, desc = "修改装饰")
public class ChangeDecorationCmd extends AbstractCommand {

	private int decorationType;

	private int decorationId;

	@Override
	public boolean parseData(YanQuMessage packet) throws Exception {
		DecorationProto.ChangeDecorationReqMsg reqMsg = DecorationProto.ChangeDecorationReqMsg.parseFrom(packet.getBytes());
		decorationType = reqMsg.getDecorationType();
		decorationId = reqMsg.getDecorationId();
		return true;
	}

	@Override
	public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
		if (decorationType != eDecorationType.ChatTitle.getValue()) {
			if (!player.getModule(DecorationModule.class).hasDecoration(decorationType, decorationId)) {
				ret = GameErrorCode.E_DECORATION_NO_FOUND;
				return;
			}
		} else {//聊天称号可以设置为0
			if (decorationId > 0 && !player.getModule(DecorationModule.class).hasDecoration(decorationType, decorationId)) {
				ret = GameErrorCode.E_DECORATION_NO_FOUND;
				return;
			}
		}

		player.getModule(PlayerModule.class).changeDecoration(decorationType, decorationId, false);
	}

	@Override
	public void sendData(GamePlayer player) {
		PlayerProto.ChangeHeadFrameRespMsg.Builder respMsg = PlayerProto.ChangeHeadFrameRespMsg.newBuilder();
		respMsg.setRet(ret);
		if(0 == ret) {
			respMsg.setDecoration(player.getUserInfo().getDecoration());
		}
		player.sendPacket(Protocol.U_DECORATION_CHANGE, respMsg);
	}

}
